#include "genomeelement.h"

GenomeElement::GenomeElement(QPointer<SageVis> sageVisPointer, MultiGeneRegionVisWidget* parentWidget, QGraphicsItem *parent ) :
    VisBaseElement( sageVisPointer,  dynamic_cast<VisBaseWidget*> (parentWidget), parent)
{
    init();
}

GenomeElement::GenomeElement(QPointer<GenomeElementDataPacket> dataPacket , QPointer<SageVis> sageVisPointer, MultiGeneRegionVisWidget* parentWidget, QGraphicsItem *parent ) :
    VisBaseElement( sageVisPointer, dynamic_cast<VisBaseWidget*> (parentWidget), parent)
{
    init();
    setDataPacket( dataPacket );
    setAcceptsHoverEvents(true);
    QString type = dataPacket->getType();
    type.toLower();
    if( type.contains("gene") || type.contains("cds") || type.contains("rna") )
        setVisible( true );
}

void GenomeElement::init()
{
    //-------HEIGHT AND POSITION INIT-----
    setHeight(parentWidget->geometry().height()*.95);
    setPos(0,0);

    //-------SET DEFAULT COLORS--------
    defaultColor.setRgb( 147.0, 215.0, 255.0 );
    givenColor.setRgb( 147.0, 215.0, 255.0 );//same at start as default color
    highlightColor.setRgb( 255.0, 242.0, 42.0 );

    //------INIT VALUES FOR DRAWING VARIABLES-----
    drawArrow = false;
    drawForward = false;
}

void GenomeElement::setDataPacket(QPointer<GenomeElementDataPacket> dataPacket)
{
    this->dataPacket = dataPacket;

    //SETUP draw based on data packet values
    QString type = dataPacket->getType().toLower();
    drawArrow = type.contains("gene")|| type.contains("cds") || type.contains("rna") ; //draw arrow if gene, cds or rna
    drawForward = dataPacket->getPosStrand(); //( isPosStrand && parent->minNT < parent->maxNT ) || ( !isPosStrand && parent->minNT > parent->maxNT );
}

int GenomeElement::getStart()
{
    return dataPacket->getStart();
}

int GenomeElement::getStop()
{
    return dataPacket->getStop();
}

void GenomeElement::processHoverEvent(QGraphicsSceneHoverEvent *event)
{
    qDebug() << " hovering over a genome element!!  " << endl;

    super::processHoverEvent(event); //call base class to ensure hover event goes through
}

void GenomeElement::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    //----prevent compiler warnings by declaring what I am not using---
    Q_UNUSED(widget);
    Q_UNUSED(option);

    //---set color---
    QColor backgroundColor;

    if( hovering )
    {
        backgroundColor.setRgb(highlightColor.red(), highlightColor.green(), highlightColor.blue(), highlightColor.alpha());
    }
    else
    {
        backgroundColor.setRgb(givenColor.red(), givenColor.green(), givenColor.blue(), givenColor.alpha());
    }
    painter->setBrush(backgroundColor);
    painter->setPen( Qt::NoPen );

    //----setup draw----
    QRectF bRect = boundingRect();
    if( drawForward )
    {
        float triangleDiff =  -bRect.width()*.25;//how far to draw rect before hitting triangle
        if( bRect.width()*.25 < 4 )
            triangleDiff = -(bRect.width());//if small gene- just draw triangle
        if( bRect.width() > 12 )
            triangleDiff = -12.0;
        bRect.adjust(0, .25*bRect.height(), triangleDiff, -.25*bRect.height()); //adjust bounding rect
        painter->drawRect(bRect);//draw

        //draw triangle
        bRect = boundingRect();
        QPointF p1( bRect.x()+bRect.width(), bRect.y()+bRect.height()/2 );
        QPointF p2( bRect.x()+bRect.width()+triangleDiff, bRect.y());
        QPointF p3( bRect.x()+bRect.width()+triangleDiff, bRect.y()+bRect.height() );
        QPolygonF poly;
        poly << p1 << p2 << p3;
        painter->drawPolygon(poly);
    }
    else //draw arrow to the left
    {
        float triangleDiff =  bRect.width()*.25;
        if( bRect.width()*.25 < 4 )
            triangleDiff = (bRect.width());
        if( bRect.width() > 12 )
            triangleDiff = 12.0;

        bRect.adjust(triangleDiff, .25*bRect.height(), 0, -.25*bRect.height());
        painter->drawRect(bRect);

        bRect = boundingRect();
        QPointF p1( bRect.x(), bRect.y()+bRect.height()/2 );
        QPointF p2( bRect.x()+triangleDiff, bRect.y());
        QPointF p3( bRect.x()+triangleDiff, bRect.y()+bRect.height() );
        QPolygonF poly;
        poly << p1 << p2 << p3;
        painter->drawPolygon(poly);
    }


//    //---paint background rect---
//    painter->setBrush(backgroundColor);
//    painter->setPen( Qt::NoPen );
//    painter->drawRect( boundingRect() );//drawRoundedRect(boundingRect(), .25*boundingRect().height(), .25*boundingRect().height() );
}


//bool drawForward = ( isPosStrand && parent->minNT < parent->maxNT ) || ( !isPosStrand && parent->minNT > parent->maxNT );

//if( drawForward )//isPosStrand )//draw arrow to the right if positive strand
//{
//    float triangleDiff =  -bRect.width()*.25;//how far to draw rect before hitting triangle
//    if( bRect.width()*.25 < 4 )
//        triangleDiff = -(bRect.width());//if small gene- just draw triangle
//    if( bRect.width() > 12 )
//        triangleDiff = -12.0;

//    bRect.adjust(0, .25*bRect.height(), triangleDiff, -.25*bRect.height()); //adjust bounding rect
//    painter->drawRect(bRect);

//    //draw triangle
//    bRect = boundingRect();
//    QPointF p1( bRect.x()+bRect.width(), bRect.y()+bRect.height()/2 );
//    QPointF p2( bRect.x()+bRect.width()+triangleDiff, bRect.y());
//    QPointF p3( bRect.x()+bRect.width()+triangleDiff, bRect.y()+bRect.height() );
//    QPolygonF poly;
//    poly << p1 << p2 << p3;
//    painter->drawPolygon(poly);

//    //drawArrow( bRect, p1, p2, p3, colorToDraw );
//}
//else //draw arrow to the left
//{
//    float triangleDiff =  bRect.width()*.25;
//    if( bRect.width()*.25 < 4 )
//        triangleDiff = (bRect.width());
//    if( bRect.width() > 12 )
//        triangleDiff = 12.0;

//    bRect.adjust(triangleDiff, .25*bRect.height(), 0, -.25*bRect.height());
//    painter->drawRect(bRect);

//    bRect = boundingRect();
//    QPointF p1( bRect.x(), bRect.y()+bRect.height()/2 );
//    QPointF p2( bRect.x()+triangleDiff, bRect.y());
//    QPointF p3( bRect.x()+triangleDiff, bRect.y()+bRect.height() );
//    QPolygonF poly;
//    poly << p1 << p2 << p3;
//    painter->drawPolygon(poly);
//}

////make sure gene label drawn in the correct orientation
////ok, this kinda sucks ... because not very easy to translate to different situations
////but, I'll have to come up with something better later
//QPointF contigPos = parentItem()->parentItem()->pos();//parent contigs position in the browser
//QRectF browserBoundingRect = parentItem()->parentItem()->parentItem()->boundingRect();
//if( contigPos.y() < browserBoundingRect.y()+geneInfo->boundingRect().height())//50 )
//{
//    geneInfo->changeDirection( 2 );
//    geneInfo->setPos( (boundingRect().x() + width)/2.0, boundingRect().y()+boundingRect().height() );
//}
//else
//{
//   geneInfo->changeDirection( 0 );
//   geneInfo->setPos( (boundingRect().x()+width)/2.0, boundingRect().y() );

//}
